<?php

namespace App\Admin\Controllers\Bureau;

use App\Admin\Actions\Grid\OrderGoodsAction;
use App\Admin\Repositories\Shop\WxShopOrderGoods;
use App\Models\Shop\WxShopOrderGoods as OrderGoodsModel;
use App\Models\Shop\WxShopOrder;
use App\Wen\Utils\Settings;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WxBureauOrderGoodsController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $app_coin_name = Settings::get('app_coin_name', '硬币');
        OrderGoodsModel::whereNull('buyer_user_id')->where('goods_type', 7)->orderBy('id', 'desc')->limit(20)->get()->map(function ($v){
            $user_id = WxShopOrder::withTrashed()->where('id', $v->order_id)->value('user_id');
            if($user_id !== null){
                OrderGoodsModel::where('id', $v->id)->update([
                    'buyer_user_id' => $user_id
                ]);
            }

        });
        return Grid::make(new WxShopOrderGoods(), function (Grid $grid) use ($app_coin_name){
            $grid->simplePaginate();
            $grid->quickSearch(['id', 'name'])->placeholder('搜索订单商品ID，名字...');
            $grid->model()->whereIn('goods_type', [7])->orderBy('id', 'desc');
            $grid->column('id')->sortable();
//            $grid->column('order_id', '站内订单id')->display(function ($v){
//                return '<a href="'.admin_url('/shop/order?id='.$v).'" target="_blank">订单'.$v.'</a>';
//            });
            $grid->column('seller', '卖家')->display(function ($v) {
                $user = $this->seller;
                if($user){
                    return '<div style="display: flex;"><img src="' . ($user['user_avatar'] ?? Settings::get('img_default', 'https://img.mini.minisns.cn/icons/dafault.png'))  . '"  style="border-radius:50px;width:30px;" />'.('<div style="display: flex;flex-direction: column;justify-content: center;margin-left: 4px;"><a target="_blank" href="'.admin_url('users?id='.$user['id']).'">'.$user['user_name'].'</a></div>' ?? '').'</div>';
                }
                return '';
            })->width('50px');
            $grid->column('buyers', '买家')->display(function ($v) {
                $user = $this->buyer;
                if($user){
                    return '<div style="display: flex;"><img src="' . ($user['user_avatar'] ?? Settings::get('img_default', 'https://img.mini.minisns.cn/icons/dafault.png'))  . '"  style="border-radius:50px;width:30px;" />'.('<div style="display: flex;flex-direction: column;justify-content: center;margin-left: 4px;"><a target="_blank" href="'.admin_url('users?id='.$user['id']).'">'.$user['user_name'].'</a></div>' ?? '').'</div>';
                }
                return '';
            })->width('50px');

            $grid->column('pic')->image('', 80);
            $grid->column('name');
            $grid->column('product')->limit(20);
            $grid->column('vip_price')->display(function ($v) use ($app_coin_name){
                if($this->credit_type == 1){
                    return '￥'.$v;
                }else{
                    return $v.$app_coin_name;
                }
            })->sortable();
            $grid->column('price')->display(function ($v) use ($app_coin_name){
                if($this->credit_type == 1){
                    return '￥'.$v;
                }else{
                    return $v.$app_coin_name;
                }
            })->sortable();
            $grid->column('recharge')->display(function ($v) use ($app_coin_name){
                if($this->credit_type == 1){
                    return '￥'.$v;
                }else{
                    return $v.$app_coin_name;
                }
            })->sortable();
            $grid->column('quantity');
            $grid->column('goods_id');
            $grid->column('product_id');
//            $grid->column('type')->using([0 => '正常购买', 1 => '补发', 2 => '赠送'])->label([
//                0 => 'green',
//                1 => 'blue',
//                2 => 'orange2'
//            ]);
            $grid->column('state')->using([0 => '无售后申请', 1 => '② 买家申请退款（待退款）', 2 => '① 卖家协商退货中（协商退货）', 3 => '③ 已退款（售后完结）', 4 => '异常'])->label([
                0 => 'silver',
                1 => 'red',
                2 => 'red',
                3 => 'green',
                5 => 'primary',
            ]);
            $grid->column('created_at');
//            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('order_id');
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append((new OrderGoodsAction(6))->setKey($this->id));
            });

            $grid->disableCreateButton();
//            $grid->disableActions();
            $grid->disableViewButton();
            // 禁用编辑按钮
            $grid->disableEditButton();
            $grid->disableDeleteButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxShopOrderGoods(), function (Show $show) {
            $show->field('id');
            $show->field('pic');
            $show->field('name');
            $show->field('product');
            $show->field('vip_price');
            $show->field('price');
            $show->field('recharge');
            $show->field('quantity');
            $show->field('order_id');
            $show->field('goods_id');
            $show->field('product_id');
            $show->field('type');
            $show->field('state');
            $show->field('created_at');
            $show->field('updated_at');
            $show->disableEditButton();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxShopOrderGoods(), function (Form $form) {
            $form->display('id');
            $form->text('pic');
            $form->text('name');
            $form->text('product');
            $form->text('vip_price');
            $form->text('price');
            $form->text('recharge');
            $form->text('quantity');
            $form->text('order_id');
            $form->text('goods_id');
            $form->text('product_id');
            $form->text('type');
            $form->text('state');

            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });

        });
    }
}
